package collections;

import sun.applet.Main;

import java.util.HashSet;

public class SetTest1 {

    public static void main(String[] args) {
//        test1();
        test2();
    }

    /**
     * 测试HashSet Contains
     *
     * hashSet里面是使用hashmap来寸对象,key是你add的对象,value是1个没有意义的dummy Object
     */
    public static void test1() {
        SetTest1_MySet1<Object> s = new SetTest1_MySet1<>();
        MyClass m1 = new MyClass();
//        MyClass m2 = new MyClass();
//        MyClass m3 = new MyClass();
        System.out.println(s.add(m1)); // true
        System.out.println(s.add(m1)); // false 因为已经存在了
    }

    public static void test2() {
        SetTest1_MySet1<Object> s = new SetTest1_MySet1<>();
        MyClass m1 = new MyClass();
        System.out.println(s.add(m1)); // true
        m1 = new MyClass();
        System.out.println(s.remove(m1)); // false
        System.out.println(s); // 1个对象

    }

    private static class SetTest1_MySet1<E> extends HashSet<E> {
        @Override
        public int hashCode() {
            System.out.println("SetTest1_MySet1 -> hashcode");
            return super.hashCode();
        }

        @Override
        public boolean equals(Object o) {
            System.out.println("SetTest1_MySet1 -> equals");
            return super.equals(o);
        }
    }

    private static class MyClass {
        @Override
        public int hashCode() {
            System.out.println("MyClass -> hashcode");
            return super.hashCode();
        }

        @Override
        public boolean equals(Object o) {
            System.out.println("MyClass- > equals");
            return super.equals(o);
        }
    }
}
